package com.yanqu.road.server.pb;

import com.yanqu.road.entity.hunt.UserHunt;
import com.yanqu.road.entity.hunt.UserHuntPatronsData;
import com.yanqu.road.entity.message.WorldMessage;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.hunt.HuntProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.config.HuntConfigMgr;
import com.yanqu.road.server.manger.player.UserMgr;

import java.util.List;

public class HuntPb {
    public static HuntProto.HuntDataMsg.Builder parseHuntDataMsg(UserHunt userHunt){
        if(null == userHunt){
            return null;
        }
        HuntProto.HuntDataMsg.Builder msg = HuntProto.HuntDataMsg.newBuilder();
        msg.setBossIndex(userHunt.getMonsterIndex());
        msg.setBossLoseBlood(userHunt.getMonsterLoseBlood().toString());
        msg.setHuntScore(userHunt.getHuntScore().toString());
        return msg;
    }

    public static HuntProto.HuntBoxDataMsg.Builder parseHuntBoxDataMsg(UserHunt userHunt){
        if(null == userHunt){
            return null;
        }
        HuntProto.HuntBoxDataMsg.Builder msg = HuntProto.HuntBoxDataMsg.newBuilder();
        msg.setOpenBoxTimes(userHunt.getOpenBoxTimes());
        for(String uid : userHunt.getOpenHuntBoxUidList()){
            msg.addOpenHuntBoxUidList(uid);
        }
        return msg;
    }

    public static HuntProto.HuntPatronsDataTempMsg.Builder parseHuntPatronsDataTempMsg(GamePlayer player, UserHuntPatronsData userHuntPatronsData){
        if(null == userHuntPatronsData){
            return null;
        }
        HuntProto.HuntPatronsDataTempMsg.Builder msg = HuntProto.HuntPatronsDataTempMsg.newBuilder();
        msg.setPatronsId(userHuntPatronsData.getPatronsId());
        int canBattleTimes = HuntConfigMgr.getPatronsMaxHuntBattleTimes(player, userHuntPatronsData.getPatronsId()) - userHuntPatronsData.getBattleTimes();
        if(canBattleTimes < 0){
            canBattleTimes = 0;
        }
        msg.setCanBattleTimes(canBattleTimes);
        return msg;
    }

    public static HuntProto.HuntPatronsDataListMsg.Builder parseHuntPatronsDataListMsg(GamePlayer player, List<UserHuntPatronsData> userHuntPatronsDataList){
        HuntProto.HuntPatronsDataListMsg.Builder listMsg = HuntProto.HuntPatronsDataListMsg.newBuilder();
        if(null != userHuntPatronsDataList){
            for(UserHuntPatronsData userHuntPatronsData : userHuntPatronsDataList){
                HuntProto.HuntPatronsDataTempMsg.Builder msg = parseHuntPatronsDataTempMsg(player, userHuntPatronsData);
                if(null != msg){
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static HuntProto.HuntMessageTempMsg.Builder parseHuntMessageTempMsg(WorldMessage worldMessage) {
        if(null == worldMessage){
            return null;
        }
        if(worldMessage.getSendTime() <= 0){
            return null;
        }
        HuntProto.HuntMessageTempMsg.Builder msg = HuntProto.HuntMessageTempMsg.newBuilder();
        msg.setType(worldMessage.getType());
        msg.setContent(worldMessage.getContent());
        msg.setSendTime(worldMessage.getSendTime());
        msg.setUserId(worldMessage.getUserId());
        UserInfo userInfo = null;
        if(worldMessage.getUserId() > 0){
            userInfo = UserMgr.getUserInfo(worldMessage.getUserId());
        }
        if(null != userInfo) {
            PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId()));
            msg.setPlayerBaseData(baseMsg);
        }
        return msg;
    }
}